// Ambient occlusion mapping fragment shader

uniform vec2 fvTextureSizes;
uniform sampler2D ambientMap;

vec4 fvGetAmbientOcclusionFactor(vec2 texCoord, float mult)
{
   vec4 fvAmbientColor = texture2D( ambientMap, texCoord );
   vec4 fvSum = vec4(0.0);
   
   if (mult != 1.0) {
       vec4 fvKernel[8];
       vec2 fvDisp = 1.0 / fvTextureSizes;
	   
       fvKernel[0] = texture2D( ambientMap, texCoord + fvDisp );
       fvKernel[1] = texture2D( ambientMap, texCoord - fvDisp );
   
       fvKernel[2] = texture2D( ambientMap, texCoord + vec2(-fvDisp.x, fvDisp.y) );
       fvKernel[3] = texture2D( ambientMap, texCoord + vec2(fvDisp.x, -fvDisp.y) );
   
       fvKernel[4] = texture2D( ambientMap, texCoord + vec2(fvDisp.x, 0.0) );
       fvKernel[5] = texture2D( ambientMap, texCoord + vec2(-fvDisp.x, 0.0) );
   
       fvKernel[6] = texture2D( ambientMap, texCoord + vec2(0.0, fvDisp.y) );
       fvKernel[7] = texture2D( ambientMap, texCoord + vec2(0.0, -fvDisp.y) );
   
       for (int i = 0; i < 8; ++i) fvSum += abs( fvKernel[i] - fvAmbientColor );
   }
   
   return fvAmbientColor * (1.0 - fvSum * (1.0 - mult));
}
